﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using HNG.LIB.Controls;
using HNG.Portal.BLL;
using HNG.Portal.DAL;
using HNG.Portal.DAL.EntityClasses;
using HNG.Portal.DAL.FactoryClasses;
using HNG.Portal.DAL.HelperClasses;
using SD.LLBLGen.Pro.ORMSupportClasses;
using System.Web.UI.HtmlControls;
using System.Data;
using System.IO;
public partial class AdminPortal_Controls_Account_Role_List : System.Web.UI.UserControl
{
    TblSiteMapManager _TblSiteMapManager = new TblSiteMapManager();
    TblThanhVienManager _TblThanhVienManager = new TblThanhVienManager();
    TblRoleManager _TblRoleManager = new TblRoleManager();
    TblThanhVienRoleManager _TblThanhVienRoleManager = new TblThanhVienRoleManager();
    int UserID = 0;
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            try { UserID = Convert.ToInt32(Request.QueryString["id"].Trim()); }
            catch { UserID = 0; }

            hidID.Value = UserID.ToString();
            txtID.Text = hidID.Value;
            FunctionExtend.LoadRloeGroup(ddlThanhVienGroup);
            TblThanhVienEntity _TblThanhVienEntity = _TblThanhVienManager.SelectOne(Convert.ToInt32(hidID.Value));
            if (_TblThanhVienEntity != null)
            {
                string strRole = _TblThanhVienEntity.Role;
                ddlThanhVienGroup.SelectedValue = strRole;
            }
            ddlSiteMapChoice.Items.Add(new ListItem("", ""));
            // load siteMap
            loadSiteMap(ddlSiteMap);
            //load siteMapChoice
            LoadSiteMapChoice();
            //ddlSiteMapChoice.Attributes.Add("onclick", "javascript:SiteMapChoiceClick();return false;");
        }
    }

    private void LoadSiteMapChoice()
    {
        DataTable _dt = _TblThanhVienRoleManager.GetSiteMapByThanhVienID(Convert.ToInt32(hidID.Value));
        if (_dt.Rows.Count > 0 && _dt != null)
        {
            foreach (DataRow r in _dt.Rows)
            {
                ListItem Item = new ListItem("--" + r[TblSiteMapFields.Title.Name].ToString(), r[TblSiteMapFields.Id.Name].ToString());
                ddlSiteMapChoice.Items.Add(Item);
            }
        }
        else
        {
            ddlSiteMapChoice.Items.Clear();
            ddlSiteMapChoice.Items.Add(new ListItem("", ""));
        }
    }
    
    private void loadSiteMap(ListBox _ddl)
    {
        TblSiteMapEntity _TblSiteMapEntity = new TblSiteMapEntity();
        _TblSiteMapEntity.SortDirection = "ASC";
        _TblSiteMapEntity.Status = "1";
        DataTable _dtDM = _TblSiteMapManager.be_SelectToDropdownlist(_TblSiteMapEntity);

        if (_dtDM != null || _dtDM.Rows.Count > 0)
        {
            HNG.LIB.BIND_SOURCE.Bind_SiteMapToListBox(_ddl, _dtDM, TblSiteMapFields.Title.Name, TblSiteMapFields.Id.Name, "RoleParent");
        }
        _ddl.SelectedIndex = 0;
    }

    protected void btnSave_Click(object sender, EventArgs e)
    {
        SaveToUserRole();
        Response.Redirect(SiteMap.CurrentNode.ParentNode.Url);
    }

    protected void btnApply_Click(object sender, EventArgs e)
    {
        SaveToUserRole();
        Response.Redirect(ResolveUrl("~/AdminPortal/Pages/RoleList.aspx?id=") + hidID.Value);
    }

    protected void btnBack_Click(object sender, EventArgs e)
    {
        Response.Redirect(ResolveUrl("~/AdminPortal/Pages/UserList.aspx"));
    }
    protected void lnkCheckAll_Click(object sender, EventArgs e)
    {
        chkAdd.Checked = chkDel.Checked =
            chkSave.Checked =
            chkApply.Checked = chkApply.Checked =
            chkBack.Checked =
            chkSearch.Checked = chkView.Checked = true;
    }
    protected void lnkUnCheckAll_Click(object sender, EventArgs e)
    {
        chkAdd.Checked = chkDel.Checked =
            chkSave.Checked =
            chkApply.Checked = chkApply.Checked =
            chkBack.Checked =
            chkSearch.Checked = chkView.Checked = false;
    }

    protected void btnIn_Click(object sender, EventArgs e)
    {
        ListItem Item = ddlSiteMap.SelectedItem;
        if (Item.Value == "0") return;
        if (ddlSiteMapChoice.Items.FindByValue(Item.Value) != null) return;
        ddlSiteMapChoice.Items.Add(Item);
    }

    protected void btnOut_Click(object sender, EventArgs e)
    {
        try
        {
            TblThanhVienRoleEntity _TblThanhVienRoleEntity = new TblThanhVienRoleEntity();
            _TblThanhVienRoleEntity.ThanhVienId = Convert.ToInt32(hidID.Value);
            _TblThanhVienRoleEntity.SiteMapId = Convert.ToInt32(ddlSiteMapChoice.SelectedValue);
            DataTable _dt = _TblThanhVienRoleManager.be_selectAllThanhVienRole(_TblThanhVienRoleEntity);
            if (_dt.Rows.Count > 0)
            {
                int RoleID = Convert.ToInt32(_dt.Rows[0][TblThanhVienRoleFields.RoleId.Name].ToString());
                _TblRoleManager.Delete(RoleID);
                if (_TblThanhVienRoleManager.DeleteThanhVienRole(_TblThanhVienRoleEntity) != -1)
                {
                    Response.Redirect(ResolveUrl("~/AdminPortal/Pages/RoleList.aspx?id=") + hidID.Value);
                }
                else
                {
                    ScriptManager.RegisterStartupScript(this, this.GetType(), "redirectMe", "showMessageSlide('Lỗi', 'Xoá quyền này không thành công!');", true);
                }
            }
        }
        catch { }
    }
    //protected void lnkGetAllSiteMap_Click(object sender, EventArgs e)
    //{
    //    ddlSiteMapChoice.Items.Clear();
    //    foreach (ListItem item in ddlSiteMap.Items)
    //    {
    //        if (item.Value != "0")
    //        {
    //            ddlSiteMapChoice.Items.Add(item);
    //        }
    //    }
    //}
    //protected void lnkUnGetAllSiteMap_Click(object sender, EventArgs e)
    //{        
    //    try
    //    {
    //        TblThanhVienRoleEntity _TblThanhVienRoleEntity = new TblThanhVienRoleEntity();
    //        _TblThanhVienRoleEntity.ThanhVienId = Convert.ToInt32(hidID.Value);

    //        DataTable _dt = _TblThanhVienRoleManager.be_selectAllThanhVienRole(_TblThanhVienRoleEntity);
    //        if (_dt.Rows.Count > 0)
    //        {
    //            foreach (DataRow r in _dt.Rows)
    //            {
    //                int RoleID = Convert.ToInt32(r[TblThanhVienRoleFields.RoleId.Name].ToString());
    //                _TblRoleManager.Delete(RoleID);
    //            }
    //            _TblThanhVienRoleManager.DeleteThanhVienRole(_TblThanhVienRoleEntity);
    //        }
    //        LoadSiteMapChoice();
    //    }
    //    catch { }
                
    //}
    protected void ddlSiteMapChoice_SelectedIndexChanged(object sender, EventArgs e)
    {        
        try {            
            BindToCheckBox(Convert.ToInt32(ddlSiteMapChoice.SelectedValue)); }
        catch { lnkUnCheckAll_Click(new object(), new EventArgs()); }        
    }

    //khi chon Item thi do dieu lieu ra
    private void BindToCheckBox(int SiteMapID)
    {
        TblThanhVienRoleEntity _TblThanhVienRoleEntity = new TblThanhVienRoleEntity();
        _TblThanhVienRoleEntity.ThanhVienId = Convert.ToInt32(hidID.Value);
        _TblThanhVienRoleEntity.SiteMapId = SiteMapID;

        DataTable _dtRole = _TblThanhVienRoleManager.be_selectAllThanhVienRole(_TblThanhVienRoleEntity);

        if (_dtRole.Rows.Count > 0)
        {
            DataRow r = (DataRow)_dtRole.Rows[0];
            if (r != null)
            {
                TblRoleEntity _TblRoleEntity = _TblRoleManager.SelectOne(Convert.ToInt32(r[TblThanhVienRoleFields.RoleId.Name].ToString()));
                if (_TblRoleEntity != null)
                {
                    chkAdd.Checked = Convert.ToBoolean(_TblRoleEntity.BtnThem);
                    chkDel.Checked = Convert.ToBoolean(_TblRoleEntity.BtnXoa);
                    //chkEdit.Checked = Convert.ToBoolean(_TblRoleEntity.BtnSua);
                    chkSave.Checked = Convert.ToBoolean(_TblRoleEntity.BtnLuuLai);
                    chkApply.Checked = Convert.ToBoolean(_TblRoleEntity.BtnCapNhat);
                    //chkRefesh.Checked = Convert.ToBoolean(_TblRoleEntity.BtnRefesh);
                    chkBack.Checked = Convert.ToBoolean(_TblRoleEntity.BtnBack);
                    chkSearch.Checked = Convert.ToBoolean(_TblRoleEntity.BtnSearch);
                    chkView.Checked = Convert.ToBoolean(_TblRoleEntity.BtnView);                    
                }
            }
        }
        else
        { 
            lnkUnCheckAll_Click(new object(),new EventArgs());
        }
    }

    //load chi tiet san pham
    protected void SaveToUserRole()
    {
        TblThanhVienEntity _TblThanhVienEntity = _TblThanhVienManager.SelectOne(Convert.ToInt32(hidID.Value));
        if (_TblThanhVienEntity != null)
        {
            _TblThanhVienEntity.Role = ddlThanhVienGroup.SelectedValue;
            _TblThanhVienManager.Update(_TblThanhVienEntity);
        }
        if (!string.IsNullOrEmpty(ddlSiteMapChoice.SelectedValue))
        {
            TblRoleEntity _TblRoleEntity = new TblRoleEntity();
            TblThanhVienRoleEntity _TblThanhVienRoleEntity = new TblThanhVienRoleEntity();
            _TblThanhVienRoleEntity.SiteMapId = Convert.ToInt32(ddlSiteMapChoice.SelectedValue);
            _TblThanhVienRoleEntity.ThanhVienId = Convert.ToInt32(hidID.Value);

            DataTable _dtRole = _TblThanhVienRoleManager.be_selectAllThanhVienRole(_TblThanhVienRoleEntity);

            //update role cho thanh vien de check dieu kien sau nay neu la Administrator            

            _TblRoleEntity.TenNhomQuyen = ddlThanhVienGroup.SelectedValue;
            _TblRoleEntity.BtnThem = chkAdd.Checked;
            _TblRoleEntity.BtnXoa = chkDel.Checked;
            //_TblRoleEntity.BtnSua = chkEdit.Checked;
            _TblRoleEntity.BtnLuuLai = chkSave.Checked;
            _TblRoleEntity.BtnCapNhat = chkApply.Checked;
            //_TblRoleEntity.BtnRefesh = chkRefesh.Checked;
            _TblRoleEntity.BtnBack = chkBack.Checked;
            _TblRoleEntity.BtnSearch = chkSearch.Checked;
            _TblRoleEntity.BtnView = chkView.Checked;
            string str=ddlSiteMapChoice.SelectedItem.Text;
            if (!string.IsNullOrEmpty(str))
            {
                str = str.Replace("--", "");
                _TblRoleEntity.TenNhomQuyen = str;
            }
            if (_dtRole.Rows.Count > 0)
            {
                DataRow r = (DataRow)_dtRole.Rows[0];
                _TblRoleEntity.Id = Convert.ToInt32(r[TblThanhVienRoleFields.RoleId.Name]);
                _TblRoleManager.Update(_TblRoleEntity);
            }
            else
            {
                _TblRoleManager.Insert(_TblRoleEntity);//lay dc ngay roleID moi them vao
                _TblThanhVienRoleEntity.RoleId = _TblRoleEntity.Id;
                _TblThanhVienRoleManager.Insert(_TblThanhVienRoleEntity);
            }
        }
    }
}